package jnpf.service.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.common.enums.ragFlow.RagFlowApiEnum;
import jnpf.model.ragFlow.common.RagFlowApiResult;
import jnpf.model.ragFlow.know.DatasetResp;
import jnpf.model.ragFlow.talkMange.SessionCreateReq;
import jnpf.model.ragFlow.talkMange.SessionDeleteReq;
import jnpf.model.ragFlow.talkMange.SessionUpdateReq;
import jnpf.service.RagTalkMangeService;
import jnpf.util.RagFlowApiReqUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static jnpf.util.ResultHandleUtil.txtConvertObj;


@Service
public class RagTalkMangeServiceImpl implements RagTalkMangeService {
    @Value("${ragflow.url:}")
    public String API_BASE_URL;
    @Value("${ragflow.knowApiKey}")
    public String API_KEY;
    @Autowired
    public UserProvider userProvider;


    @Override
    public RagFlowApiResult createSession(String chatId, SessionCreateReq request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("chat_id", chatId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY,
                RagFlowApiEnum.CREATE_SESSION, pathParams, request,userProvider.get().getUserId()), DatasetResp.class);
    }

    @Override
    public RagFlowApiResult updateSession(String chatId, String sessionId, SessionUpdateReq request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("chat_id", chatId);
        pathParams.put("session_id", sessionId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY,
                RagFlowApiEnum.UPDATE_SESSION, pathParams, request, userProvider.get().getUserId()),DatasetResp.class);
    }

    @Override
    public RagFlowApiResult deleteSessions(String chatId, SessionDeleteReq request) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("chat_id", chatId);
        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY,
                RagFlowApiEnum.DELETE_SESSIONS, pathParams, request, userProvider.get().getUserId()),DatasetResp.class);
    }

    @Override
    public RagFlowApiResult listSessions(String chatId, Integer page, Integer pageSize,
                                         String orderby, Boolean desc, String name, String id) throws Exception {
        Map<String, String> pathParams = new HashMap<>();
        pathParams.put("chat_id", chatId);

        Map<String, String> queryParams = new HashMap<>();
        if (page != null) queryParams.put("page", page.toString());
        if (pageSize != null) queryParams.put("page_size", pageSize.toString());
        if (orderby != null) queryParams.put("orderby", orderby);
        if (desc != null) queryParams.put("desc", desc.toString());
        if (name != null) queryParams.put("name", name);
        if (id != null) queryParams.put("id", id);

        return txtConvertObj(RagFlowApiReqUtil.sendRequest(API_BASE_URL, API_KEY,
                RagFlowApiEnum.LIST_SESSIONS, pathParams, queryParams, userProvider.get().getUserId()),DatasetResp.class);
    }


}
